/****************************************************************************
**
** Authors: Antonio Marcio A Menezes, Emanuel Grohs.
**
** This file is part of the Qt Board Game Framework.
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include <QGraphicsItem>
#include "zoneg.h"

using namespace qtbf;

ZoneG::ZoneG(QGraphicsItem *parent) :
    QGraphicsRectItem(parent)
{
}

/* Layout this zone content. */
// TODO deve ser recursivo? in-out ou out-in?
public void layout(/* Container comp */) {
        int nro = this.getComponentCount();
        // if (nro > 0)
        {
                TitledBorder t = (TitledBorder) this.getBorder();
                Dimension min = (t == null ? new Dimension() : t.getMinimumSize(this));
                Dimension pref = new Dimension((nro + 1) * GUIPreferencias.minDistX + GUIPreferencias.deckX, (nro + 2)
                                * GUIPreferencias.minDistY + GUIPreferencias.deckY);
                this.setPreferredSize(new Dimension(min.width > pref.width ? min.width : pref.width,
                                min.height > pref.height ? min.height : pref.height));
                for (int i = 0; i < nro; i++) {
                        Zone Zone = (Zone) this.getComponent(i);
                        Zone.setLocation((nro - i) * GUIPreferencias.minDistX, (nro - i + 1) * GUIPreferencias.minDistY);
                        // setComponentZOrder(Zone, nro-1);
                }
        }
        revalidate();
        repaint();
        // for (Component c : comp.getComponents()) {
        // layout((Zone) c, compress);
        // }
}

/* Draw a border around this zone. */
public static void createBorder(Zone Zone, boolean border) {
        if (Zone != null && Zone.getIcon() != null) {
                if (border) {
                        Zone.setSize(Zone.getIcon().getIconWidth() + GUIPreferencias.larguraBorda * 2, Zone.getIcon()
                                        .getIconHeight()
                                        + GUIPreferencias.larguraBorda * 2);
                        Zone.setBorder(BorderFactory.createLineBorder(GUIPreferencias.corBorda, GUIPreferencias.larguraBorda));
                        // EtchedBorder(EtchedBorder.RAISED));//BevelBorder(BevelBorder.RAISED));//
                } else {
                        Zone.setBorder(null);
                        Zone.setSize(Zone.getIcon().getIconWidth(), Zone.getIcon().getIconHeight());
                }
        }
}

public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        if (selected) {
                negative(g2, this);
        } else {
                super.paint(g);
        }
}

/* Invert zone colors. */
private void negative(Graphics g, Zone Zone) {
        RescaleOp op = new RescaleOp(-1.0f, 255f, null);
        Graphics2D g2 = (Graphics2D) g;
        BufferedImage bi = GUIPreferencias.toBufferedImage(((ImageIcon) Zone.getIcon()).getImage());
        g2.drawImage(bi, op, 0, 0);
}
